<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:include="base/include/head">
</head>
<body>
<div id="cc" class="easyui-layout" style="width:100%;height:400px;">   
    <div data-options="region:'west',title:'持有角色',split:true,tools:'#tools'" style="width:200px;">
    	<div class="easyui-panel" style="height: 180px;width:100%" title="个人角色" >
    		<ul id="tree"></ul>
    	</div>
    	<div class="easyui-panel" style="height: 180px;width:100%" title="默认角色" >
    		<ul id="default_tree"></ul>
    	</div>
    	
    </div>   
    <div data-options="region:'center',title:'用户信息模块'" style="padding:5px;background:#eee;">
    	<div id="tb" style="padding:5px;height:auto">
			名字或工号：<input id="userMsg" class="easyui-textbox" style="height: 21px;width:180px" value="" />
			<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="dataTable()"> 搜索 </a>
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="addPower()">添加权限</a> 
<!-- 			<a class="easyui-linkbutton" iconCls="icon-excel" onclick="downloadExcel()">导出excel</a> -->
		</div>
		<table id="tt"></table> 
    </div>  
<!--     <div data-options="region:'east',title:'个人字段权限',split:true,tools:'#tools_userpower'" style="width:300px;">
    	<ul id="userPower_tree"></ul>
    </div>  -->
</div> 
<div id="tools">
	<!-- <a href="#" class="icon-save" onclick="javascript:alert('edit')"></a> -->
</div>

<div id="mm" class="easyui-menu" style="width:120px;">
	<div data-options="iconCls:'icon-remove',name:'remove'">移除</div>
</div>

<div id="userPower_mm" class="easyui-menu" style="width:120px;">
	<div data-options="iconCls:'icon-edit',name:'edit'">修改</div>
	<div data-options="iconCls:'icon-remove',name:'remove'">移除</div>
</div>

<div id="tools_userpower">
	<a href="javascript:editUserColumnPower()" class="icon-add" ></a>
</div>
	<div id="dd"></div> 
</body>
<script type="text/javascript">
/*<![CDATA[*/
$(function(){
	dataTable();
	$("#cc").layout('resize',{   
        height:($(window).height()-20)   
    })
    $("#cc").layout('panel','west').panel("resize",{height:($(window).height()-40)});
	$("#cc").layout('panel','center').panel("resize",{height:($(window).height()-40)});
	$("#cc").layout('panel','east').panel("resize",{height:($(window).height()-40)});
});

// 添加用户角色权限
function addPower(){
	var row = $('#tt').datagrid('getSelected');
	if(row){
		$('#dd').dialog({    
		    title: '[添加权限]--'+row.userAccount,    
		    width: 400,    
		    height: 200,    
		    closed: false,    
		    cache: false,    
		    href: '../supplier/addPowerPage',    
		    modal: true   
		});
	}else{
		$.messager.alert('警告','请选择人员！');
	}
	
}

//添加用户角色权限
function editUserColumnPower(id){
	var row = $('#tt').datagrid('getSelected');
	if(row){
		$('#dd').dialog({    
		    title: '[个人字段权限]--'+row.userAccount,    
		    width: 400,    
		    height: 200,    
		    closed: false,    
		    cache: false,    
		    href: '../user/editUserColumnPowerPage?id='+id+"&userId="+row.id,    
		    modal: true   
		});
	}else{
		$.messager.alert('警告','请选择人员！');
	}
	
}

function dataTable(){
	var userMsg = $("#userMsg").textbox('getValue');
	// 查询页面信息 提交信息
	$('#tt').datagrid(
				{
					title : '用户信息',
					toolbar : '#tb',
					url : '../supplier/queryUser',
					method : 'POST',
					queryParams : {
						userMsg : userMsg
					},
					striped : true,
					fitColumns : false,
					singleSelect : true,
					rownumbers : true,
					pagination : true,
					nowrap : true,
					striped:true,
				    remoteSort: true,
				    multiSort: true,
					pageSize : 10,
					pageList : [10, 20, 50, 100, 150, 200],
					showFooter : true,
					checkOnSelect :false,
					columns : [ [
							/* {field : 'ck', checkbox : true }, */
							{field : 'userAccount',title : '账号',		width : 200,align : 'center'},
							{field : 'userName',title : '供应商名称',	width : 200,align : 'center'},
							{field : 'password',title : '密码（加密后的）',	width : 150,align : 'center'}
							] ],
					onClickRow: function(rowIndex, rowData){
						// 加载个人权限
						loadPowers(rowData.id);
						// 加载部门权限
						loadDepartmentRole(rowData.id);
						// 加载用户字段权限
						loadUserColomnPower(rowData.id);
					} 
				}); 
	}	

// 加载用户权限信息
function loadPowers(id){
	$('#tree').tree({
		url:'../supplier/queryPowerTree?userId='+id,
		cascadeCheck :true,
		onlyLeafCheck: false,
		lines:true,
		onClick:function(node){
		},
		onContextMenu: function(e, node){
			e.preventDefault();
			// 查找节点
			$('#tree').tree('select', node.target);
			// 显示快捷菜单
			$('#mm').menu('show', {
				left: e.pageX,
				top: e.pageY,
				onClick: function(item){
					$.messager.confirm('确认', '您确定要删除角色信息吗？', function(r){
						if (r){
							var user_id = $('#tt').datagrid('getSelected').id;
							var role_id = node.id;
							var url ="../user/delPersonalRole";
							$.ajax({
								   type:"POST",
								   url:url,
								   data:{
									   userId : user_id ,
									   roleId : role_id
								   },
								   dataType : "json",
								   async: false,
								   success:function(data){
									   if(data.status){
										   loadPowers(user_id);
									   }else{
										   $.messager.alert('警告',data.msg);
									   }
									   
								   },
								   error : function(e) {
									   $.messager.alert('警告','异常！');
									}
								  });
						}
					});
				}
			});
		}

	}); 
}

// 加载部门权限信息
function loadDepartmentRole(id){
	$('#default_tree').tree({
		url:'../supplier/queryDefaultPowerTree?userId='+id,
		cascadeCheck :true,
		onlyLeafCheck: false,
		lines:true
	}); 
}

//加载部门权限信息
function loadUserColomnPower(id){
	$('#userPower_tree').tree({
		url:'../user/queryUserColumnPowerTree?userId='+id,
		cascadeCheck :true,
		onlyLeafCheck: false,
		lines:true,
		onContextMenu: function(e, node){
			e.preventDefault();
			// 查找节点
			$('#userPower_tree').tree('select', node.target);
			// 显示快捷菜单
			$('#userPower_mm').menu('show', {
				left: e.pageX,
				top: e.pageY,
				onClick: function(item){
					if(item.name == 'remove'){
						$.messager.confirm('确认', '您确定要删除字段权限信息吗？', function(r){
							if (r){
								var url ="../user/delUserColumnPower";
								$.ajax({
									   type:"POST",
									   url:url,
									   data:{
										   id : node.id
									   },
									   dataType : "json",
									   async: false,
									   success:function(data){
										   if(data.status){
											   loadUserColomnPower(id);
										   }else{
											   $.messager.alert('警告',data.msg);
										   }
									   },
									   error : function(e) {
										   $.messager.alert('警告','异常！');
										}
									  });
							}
						});
					}else if(item.name == 'edit'){
						editUserColumnPower(node.id);
					}
				}
			});
		}
	}); 
}


//添加角色权限
function addRole(){
	var user_id = $('#tt').datagrid('getSelected').id;
	var role_id = $('#addPersonalRoles').combobox('getValue');
	var url ="../supplier/addPersonalRole";
	$.ajax({
		   type:"POST",
		   url:url,
		   data:{
			   userId : user_id ,
			   roleId : role_id
		   },
		   dataType : "json",
		   async: false,
		   success:function(data){
			   if(data.status){
				   loadPowers(user_id);
				   $.messager.alert('通知','完成！'); 
			   }else{
				   $.messager.alert('通知',data.msg); 
			   }
		   },
		   error : function(e) {
			   $.messager.alert('警告','异常！');
			}
		  });
}

function addUserColumnPower(url){
	// 保存更新菜单信息
	 $.ajax({
		   type:"POST",
		   url:url,
		   data:$('#winForm').serialize(),
		   dataType : "json",
		   async: false,
		   success:function(data){
			   if(data.status){
				   $.messager.alert('通知','完成！');
				   var user_id = $('#tt').datagrid('getSelected').id;
				   loadUserColomnPower(user_id);
			   }else{
				   $.messager.alert('校验',data.msg[0].defaultMessage);
			   }
		   },
		   error : function(e) {
			   alert("确认失败！");
			}
		  });
}
/*]]>*/
</script>
</html>